/**
 * 角色相关管理
 * @author LiJunYi
 * @create 2019/7/5
 * @since 1.0.0
 */
package com.ljy.system.marvel_sys.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ljy.system.marvel_sys.system.model.role.Role;
import com.ljy.system.marvel_sys.system.model.role.RolePermission;
import com.ljy.system.marvel_sys.system.service.IPermissionService;
import com.ljy.system.marvel_sys.system.service.IRolePermissionService;
import com.ljy.system.marvel_sys.system.service.IRoleService;
import com.ljy.system.marvel_sys.common.util.dtree.DtreeUtil;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    IRoleService roleService;
    @Autowired
    IPermissionService permissionService;
    @Autowired
    IRolePermissionService rolePermissionService;
    @Resource
    RedisTemplate<String,Object> redisTemplate;

    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> listRoles(int limit,int page){
        IPage<Role> roleIPage = roleService.pageRoles(limit, page);
        Map<String,Object> resultMap = new HashMap<>(100);
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("data",roleIPage.getRecords());
        resultMap.put("count",roleIPage.getTotal());
        return resultMap;
    }


    /**树形权限展示*/
    @RequestMapping("permission/{roleId}")
    @ResponseBody
    public DtreeUtil listDtree(@PathVariable("roleId") int roleId){
        return permissionService.queryPermissionByRoleId(roleId);
    }

    /**保存该角色菜单权限*/
    @RequiresRoles(value = "admin")
    @RequestMapping("save_permission/{roleId}")
    @ResponseBody
    public boolean savePermissionByRoleId(@PathVariable("roleId") int roleId,String[] permissionIds){
        String[] pIds = permissionIds;
        /*先删除该角色所有菜单权限*/
        rolePermissionService.remove(new QueryWrapper<RolePermission>().lambda().eq(RolePermission::getRoleId,roleId));
        /*删除后进行批量插入*/
        List<RolePermission> item = new ArrayList<>();
        for(String str : pIds){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(Long.parseLong(str));
            item.add(rolePermission);
        }
        boolean flag = rolePermissionService.saveBatch(item);
        // 清除登录时用户对应的角色缓存
        String key = "roleId_"+roleId + "_redisPermissionRoot";
        redisTemplate.delete(key);
        // 清除对应角色的DtreeUtil树形结构缓存
        redisTemplate.delete("roleId_"+roleId+"_redisDtreeUtil");
        return flag;
    }
}
